<extend name="public/form"/>
<block name="css"></block>
<block name="content">
    <div class="layui-card-header "></div>
    <div class="layui-card-body ">
        <form id="form1" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" autocomplete="off" class="layui-input " value="{{$user.username}}" disabled>
                </div>
            </div>
            <input type="hidden" name="id" value="{{$user.id}}">
            <div class="layui-form-item" pane="">
                <label class="layui-form-label">授权角色</label>
                <div class="layui-input-block">
                    <volist name="groups" id="vo" >
                        <input type="checkbox" name="groups[]" {{$vo.checked}} value="{{$vo.id}}" lay-skin="primary" title="{{$vo.title}}">
                    </volist>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</block>
<block name="js">
    <script>
        layui.use([ 'jquery', 'form', 'layer', 'tree', 'util', 'element',], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer;
            //监听提交
            form.render();
            form.on('submit(add)', function (data) {
                let send_json = data.field;
                //   console.log(send_json);  return false;
                url='/admin/user/authUser/id/'+send_json.id;
                delete send_json.id;
                // let ids=[];
                // for(let key in send_json){
                //     ids.push(send_json[key]);
                // }
                $.ajax({
                    url: url,
                    method: 'post',
                    data:send_json,
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == 0) {
                            layer.alert("授权成功成功", {
                                icon: 6
                            }, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload();
                                //关闭当前frame
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });
                return false;
            });

            //遍历select option
            $(document).ready(function () {

            });
        });
    </script>
</block>